;;; custom-post.el --- Override default config

;;; Commentary:

;;; Code:

;; --- Not display frame-title ---
;; un-display frame-title
;; use tab as header
(set-frame-parameter (car (frame-list)) 'undecorated t)

;; --- awesome-tab ---
;; manateelazycat/awesome-tab
(add-to-list 'load-path (expand-file-name "~/.centaur/modules/lazycat-awesome-tab"))
(require 'awesome-tab)
(awesome-tab-mode t)
(awesome-tab-build-helm-source)

;; Keybindings for selecting tab with awesome-tab
(bind-key* "s-n" 'awesome-tab-forward)
(bind-key* "s-p" 'awesome-tab-backward)
;; (bind-key* "M-C-n" 'tabbar-forward-group)
;; (bind-key* "M-C-p" 'tabbar-backward-group)
(bind-key* "C-x K" 'awesome-tab-kill-all-buffers-in-current-group)

;; --- mouse button ---
;; disable left mouse button click paste
(setq  mouse-yank-at-point t)
;; disable middle mouse button click
(global-unset-key (kbd "<mouse-2>"))
;; disable the right mouse button and double click to cut
(fset 'mouse-save-then-kill 'ignore)

;; --- default config ---
;; change default quoted char radix 8, alter option:10/16
(setq read-quoted-char-radix 16)

;; Not save abbrevs
(setq save-abbrevs nil)

;; --- auto update packages ---
(use-package auto-package-update
  :config
  (setq auto-package-update-delete-old-versions t)
  (setq auto-package-update-hide-results t)
  (auto-package-update-maybe))

;; --- open dot file ---
(defvar Hesperus-centaur-custom-post "~/.centaur/custom-post.el")
(defun Hesperus-open-custom-post ()
  "Open custom.el in Centaur Emacs path."
  (interactive)
  (find-file Hesperus-centaur-custom-post)
  (current-buffer))
(bind-key* "C-c d" #'Hesperus-open-custom-post)
(bind-key* "s-d" #'Hesperus-open-custom-post)

(defun Hesperus-open-newline-inline ()
  "Open a newline even if the cursor is Not at end."
  (interactive)
  (end-of-visual-line)
  (newline-and-indent))
(dolist (HA-newline-inline-hook
         (list
          'markdown-mode-hook
          'prog-mode-hook))
  (add-hook HA-newline-inline-hook
            (lambda ()
              (local-set-key (kbd "C-j")
                             #'Hesperus-open-newline-inline))))

;; --- delete empty lines ---
;; @mayingjun via emacs-cn
(defun Hesperus-delete-empty-line (beg end)
  "Delete empty lines if selected, otherwise the whole buffer."
  (interactive "r")
  (let ((empty-line "^[\t  ]*$"))
    (save-excursion
      (if (region-active-p)
          (flush-lines empty-line beg end)
        (flush-lines empty-line (point-min) (point-max))))))

;; --- split window right/below ---
;; via emacs-china.org/t/topic/945/2
(defun Hesperus-split-window-right ()
  "Split window with another buffer."
  (interactive)
  (select-window (split-window-right))
  (switch-to-buffer (other-buffer)))

(defun Hesperus-split-window-below ()
  "Split window with another buffer."
  (interactive)
  (select-window (split-window-below))
  (switch-to-buffer (other-buffer)))

(bind-key* "C-x 3" #'Hesperus-split-window-right)
(bind-key* "C-x 2" #'Hesperus-split-window-below)

;; --- auto-save ---
;; auto-save via manateelazycat/lazycat-emacs/site-lisp/extensions/lazycat/auto-save.el
(require 'auto-save)
(auto-save-enable)
(setq auto-save-slient t)
(setq auto-save-delete-trailing-whitespace t)

;; --- color-rg using ripgrep ---
(add-to-list 'load-path "~/.centaur/modules/lazycat-color-rg")
(require 'color-rg)
(bind-key* "C-c F" 'color-rg-search-project)
(bind-key* "s-F" 'color-rg-search-project)

;; --- update english edit ---
;; by using company-english-helper & insert-translated-name
(add-to-list 'load-path (expand-file-name "~/.centaur/modules/lazycat-company-english-helper"))
(require 'company-english-helper)

;; toggle of Writting with company-english-helper
(global-set-key (kbd "C-c e") 'toggle-company-english-helper)
(global-set-key (kbd "s-e") 'toggle-company-english-helper)

(add-to-list 'load-path (expand-file-name "~/.centaur/modules/lazycat-insert-translated-name"))
(require 'insert-translated-name)

;; Insert-translated-name
(global-set-key (kbd "C-c i") 'insert-translated-name-insert)
(global-set-key (kbd "s-i") 'insert-translated-name-insert)

;; --- youdao-dict ---
;; keybindings for youdao-dict
(bind-key* "C-c t" #'youdao-dictionary-search-at-point-tooltip)
(bind-key* "s-t" #'youdao-dictionary-search-at-point-tooltip)

;; --- pangu-spacing ---
(use-package pangu-spacing
  :init
  (global-pangu-spacing-mode -1)
  (add-hook 'org-mode-hook #'pangu-spacing-mode))

;; --- ess & polymode ---
(use-package ess
  :init
  (require 'ess-site))

(fset 'HA-macro-R-define
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item '(" <- " 0 "%d") arg)))
(bind-key* "M--" #'HA-macro-R-define)

;; ;; lsp for R
;; (lsp-define-stdio-client lsp-R "R"
;;                          (lambda () default-directory)
;;                          '("R" "--slave" "-e" "languageserver::run()"))
;; (add-hook 'R-mode-hook #'lsp-R-enable)

(use-package polymode)
(use-package poly-markdown)
(use-package poly-R)
(require 'poly-markdown)
(require 'poly-R)

;; --- update python-mode ---
;; set anaconda's ipython as default python interpreter
(setq python-shell-interpreter "/opt/anaconda/bin/ipython")

;; run IPython interpreter
(defun Hesperus-ipython ()
  "Open Python interpreter in Emacs terminal."
  (interactive)
  (Hesperus-split-window-right)
  (ansi-term "/opt/anaconda/bin/ipython"))

(bind-key* "C-c I" 'Hesperus-ipython)
(bind-key* "s-I" 'Hesperus-ipython)

(defun Hesperus-py-run ()
  "Run .py file."
  (interactive)
  (shell-command
   (format "%s %s" python-shell-interpreter
           (buffer-file-name (current-buffer)))))

(defun Hesperus-py-send-line ()
  "将当前行发送到 python shell.
发送前会对删除前后空格，主要使用场景是将 doctest 代码发送到 shell 进行测试。"
  (interactive)
  (python-shell-send-string (string-trim (thing-at-point 'line))))

(defun Hesperus-py-operator-comma ()
  (interactive)
  (insert ", "))

(defun Hesperus-py-operator-equal ()
  (interactive)
  (insert " = "))

(add-hook 'python-mode-hook
          (lambda ()
            ;; submit current buffer
            (local-set-key (kbd "<f8>") 'Hesperus-py-run)
            (local-set-key (kbd "C-x C-e") 'Hesperus-py-send-line)
            (local-set-key (kbd ",") #'Hesperus-py-operator-comma)
            (local-set-key (kbd "=") #'Hesperus-py-operator-equal)))

;; --- update tex-mode and macrOs ---

;; macrOs for LaTeX-mode
(fset 'HA-macro-TeX-frame
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item '([return 92 98 101 103 105 110 123 102 114 97 109 101 6 123 6 return 92 98 101 103 105 110 123 98 108 111 99 107 6 123 6 return return 92 101 110 100 123 98 108 111 99 107 6 return 92 101 110 100 123 102 114 97 109 101 6 return 16 16 16 tab] 0 "%d") arg)))

(fset 'HA-macro-TeX-block
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item '([5 return 92 98 101 103 105 110 123 98 108 111 99 107 6 123 6 return return 92 101 110 100 123 98 108 111 99 107 6 tab 16 tab] 0 "%d") arg)))

(fset 'HA-macro-TeX-indent
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item '("\\setlength{\\parindent{1.6em" 0 "%d") arg)))

(fset 'HA-macro-TeX-figure
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item '([tab 92 98 101 103 105 110 123 102 105 103 117 114 101 6 123 6 return 92 99 101 110 116 101 114 105 110 103 return 92 105 110 99 108 117 100 101 103 114 97 112 104 105 99 115 91 104 101 105 103 104 116 61 92 116 101 120 116 104 101 105 103 104 116 6 123 6 return 92 101 110 100 123 102 105 103 117 114 101 6 tab 16 134217830 134217830 6] 0 "%d") arg)))

(bind-key* "C-c o f" #'HA-macro-TeX-frame)
(bind-key* "C-c o b" #'HA-macro-TeX-block)
(bind-key* "C-c o i" #'HA-macro-TeX-indent)
(bind-key* "C-c o g" #'HA-macro-TeX-figure)

(bind-key* "s-o f" #'HA-macro-TeX-frame)
(bind-key* "s-o b" #'HA-macro-TeX-block)
(bind-key* "s-o i" #'HA-macro-TeX-indent)
(bind-key* "s-o g" #'HA-macro-TeX-figure)

;; --- paredit ---
(require 'paredit)

(dolist (Hesperus-paredit-hook
         (list
          'c-mode-common-hook
          'c-mode-hook
          'c++-mode-hook
          'java-mode-hook
          'haskell-mode-hook
          'emacs-lisp-mode-hook
          'lisp-interaction-mode-hook
          'lisp-mode-hook
          'maxima-mode-hook
          'ielm-mode-hook
          'sh-mode-hook
          'makefile-gmake-mode-hook
          'php-mode-hook
          'python-mode-hook
          'js-mode-hook
          'go-mode-hook
          'qml-mode-hook
          'jade-mode-hook
          'css-mode-hook
          'ruby-mode-hook
          'coffee-mode-hook
          'rust-mode-hook
          'qmake-mode-hook
          'lua-mode-hook
          'swift-mode-hook
          'minibuffer-inactive-mode-hook
          ))
  (add-hook 'Hesperus-paredit-hook '(lambda () (paredit-mode 1))))

(bind-key* "M-[" 'paredit-backward-slurp-sexp)
(bind-key* "M-]" 'paredit-forward-slurp-sexp)
(bind-key* "M-{" 'paredit-backward-barf-sexp)
(bind-key* "M-}" 'paredit-forward-barf-sexp)

(bind-key* "C-c s [" 'paredit-backward-slurp-sexp)
(bind-key* "C-c s ]" 'paredit-forward-slurp-sexp)
(bind-key* "C-c s {" 'paredit-backward-barf-sexp)
(bind-key* "C-c s }" 'paredit-forward-barf-sexp)
(bind-key* "C-c s <" 'paredit-splice-sexp-killing-backward)
(bind-key* "C-c s >" 'paredit-splice-sexp-killing-forward)

(bind-key* "s-s [" 'paredit-backward-slurp-sexp)
(bind-key* "s-s ]" 'paredit-forward-slurp-sexp)
(bind-key* "s-s {" 'paredit-backward-barf-sexp)
(bind-key* "s-s }" 'paredit-forward-barf-sexp)
(bind-key* "s-s <" 'paredit-splice-sexp-killing-backward)
(bind-key* "s-s >" 'paredit-splice-sexp-killing-forward)

;; ;; Note: in default,
;; (bind-key* "M-s" 'paredit-splice-sexp)
;; (bind-key* "M-S" 'paredit-split-sexp)
;; (bind-key* "M-J" 'paredit-join-sexps)

;; --- Bio data ---
(add-to-list 'load-path (expand-file-name "~/.centaur/modules/davep-fasta"))
(require 'fasta)

;; --- fonts ---
;; blog.csdn.net/xh_acmagic/article/details/78939246
(defun Hesperus-better-font ()
  (interactive)
  ;; english font
  (if (display-graphic-p)
      (progn
        (set-face-attribute 'default nil :font (format "%s:pixelsize=%d" "Source Code Pro" 14)) ;; 11 13 17 19 23
        ;; chinese font, same to chinese-fonts-setup, cnfonts
        (dolist (charset '(kana han symbol cjk-misc bopomofo))
          (set-fontset-font (frame-parameter nil 'font)
                            charset
                            (font-spec :family "Source Han Sans CN" :size 16)))) ;; 14 16 20 22 28
    ))

(defun Hesperus-init-font (frame)
  (with-selected-frame frame
    (if (display-graphic-p)
        (Hesperus-better-font))))

(if (and (fboundp 'daemonp) (daemonp))
    (add-hook 'after-make-frame-functions #'Hesperus-init-font)
  (Hesperus-better-font))

;; --- keybindings ---

;; switch diff Modes
(bind-key* "C-c m e" 'emacs-lisp-mode)
(bind-key* "C-c m l" 'latex-mode)
(bind-key* "C-c m m" 'markdown-mode)
(bind-key* "C-c m o" 'org-mode)
(bind-key* "C-c m p" 'python-mode)
(bind-key* "C-c m P" 'prettify-symbols-mode)
(bind-key* "C-c m r" 'R-mode)
(bind-key* "C-c m t" 'tex-mode)
(bind-key* "C-c m x" 'text-mode)
(bind-key* "C-c m y" 'yaml-mode)

;; same function with Super key
(bind-key* "s-m e" 'emacs-lisp-mode)
(bind-key* "s-m l" 'latex-mode)
(bind-key* "s-m m" 'markdown-mode)
(bind-key* "s-m o" 'org-mode)
(bind-key* "s-m p" 'python-mode)
(bind-key* "s-m P" 'prettify-symbols-mode)
(bind-key* "s-m r" 'R-mode)
(bind-key* "s-m t" 'tex-mode)
(bind-key* "s-m x" 'text-mode)
(bind-key* "s-m y" 'yaml-mode)

;; Describe-char and codepoint
(bind-key* "C-c D" 'describe-char)
(bind-key* "s-D" 'describe-char)

;; Find file in project
(bind-key* "C-c f" 'projectile-find-file)
(bind-key* "s-f" 'projectile-find-file)

;; popup maGit status
(bind-key* "C-c g" 'magit-status)
(bind-key* "s-g" 'magit-status)

;; Jump with avy-goto-char
(bind-key* "C-c j" 'avy-goto-char)
(bind-key* "s-j" 'avy-goto-char)

(bind-key* "C-r" 'anzu-query-replace-regexp)

;; remap M-^ to M-6 (Not type shift)
(bind-key* "M-6" 'delete-indentation)

;; --- Not display killing async process confirm ---
(setq doom-modeline-github nil)
